<?php
namespace Mobile\Controller;
use Think\Controller;
use Api\Controller\ImageController;

class ModelMessageController extends Controller{
    
    private $www_url;   //域名
    private $seller;
    
    public function _initialize(){
        $this->www_url = C('WWW_ROOT');
        $this->seller = new SellerController();
    }
  
    //[保留]0--已申请[待确认]1--申请取消2--申请被拒绝3--已确认[待付款/进行中]4--
    //已付款到平台[等待到期平台打钱给模特/进行中【托管】]5--平台已付款给模特[待评价/已完成]6--已评价7--卖家逻辑删除8
    /**
     * 模特获取肖像权消息
     * @param type $state       状态：0-待确认；1-进行中；2-已完成
     * @param type $uid         模特id
     * @param type $per_page    分页，单页条目数（get方式）
     * 获取消息基本信息，同时将消息置为已读
     */
    public function getPortraitMsg($uid=0, $per_page=3){
        
        if($uid == 0){
            $this->seller->returnAjax(0, '用户id不能为空');
        }
        $page_data = page('Apply_portrait', $per_page, array('mid' => $uid), array('id' => 'desc'), 
                '', 'id, sid, month_count, total_money, begin_date, end_date, status');
        $data = $page_data['list'];
        //遍历消息获取模特信息
        if(!empty($data)){
            foreach ($data as $key => $value){
                $user = M('User')->field('avatar_url,nickname')->where(array('id' => $value['sid']))->find();
                $user['avatar_url'] = $this->www_url . $user['avatar_url'];
                $data[$key]['begin_date'] = date("Y-m-d", $value['begin_date']);
                $data[$key]['end_date'] = date("Y-m-d", $value['end_date']);
                $data[$key] = array_merge($data[$key], $user);
            }
            $this->ajaxReturn(array(
                'code'  => 1,
                'msg'   => '消息获取成功',
                'data'  => $data,
                'total_count' => $page_data['total'],
            ));
        }
        $this->seller->returnAjax(0, '暂无消息');
    }
    
    /**
     * 模特拒绝卖家申请的肖像权
     * @param type $apply_id 申请记录id
     */
    public function refuseApply($apply_id=0){
        if($apply_id == 0){
            $this->seller->returnAjax(0, '接口参数错误');
        }
        $apply = M('Apply_portrait')->where(array('id' => $apply_id, 'status' => 1))->find();
        if(!empty($apply)){
            M('Apply_portrait')->where(array('id' => $apply_id))->save(array('status' => 3));
            $this->seller->sendMsg($apply['uid'], 1, '模特拒绝了您申请的肖像权');
            $this->seller->returnAjax(1, '拒绝成功');
        }
        $this->seller->returnAjax(0, '已被卖家取消或该记录不存在');
    }

    /**
     * 模特确认接受卖家申请的肖像权
     * @param type $apply_id 申请记录id
     */
    public function confirmApply($apply_id=0){
        if($apply_id == 0){
            $this->seller->returnAjax(0, '接口参数错误');
        }
        $apply = M('Apply_portrait')->where(array('id' => $apply_id, 'status' => 1))->find();
        if(!empty($apply)){
            M('Apply_portrait')->where(array('id' => $apply_id))->save(array('status' => 4));
            $this->seller->sendMsg($apply['sid'], 1, '模特已接受您申请的肖像权');
            $this->seller->returnAjax(1, '确认成功');
        }
        $this->seller->returnAjax(0, '确认失败');
    }

    /**  模特体验消息  **/
    //体验状态：0-[保留]；1-已申请[待模特确认]；2-卖家取消邀请；3-模特拒绝邀请；4-模特已确认并付押金[待付款/进行中]；
    //5-卖家已付款[托管]；6-报告已提交[待卖家确认]；7-确认订单/查看报告/交易成功[完成]
    /**
     * 模特获取体验消息
     * @param type $state       状态：0-待确认；1-进行中；2-已完成
     * @param type $uid         卖家id
     * @param type $per_page    分页，单页条目数（get方式）
     * 获取消息基本信息，同时将消息置为已读
     */
    public function getExperienceMsg($uid=0, $per_page=3){
        
        if($uid == 0){
            $this->seller->returnAjax(0, '用户uid不能为空');
        }
        $page_data = page('InviteExperience', $per_page, array('mid' => $uid), array('id' => 'desc'), 
                '', 'id, sid, total_fee, end_time, sub_time, status');
        $data = $page_data['list'];
        //遍历消息获取模特信息
        if(!empty($data)){
            foreach ($data as $key => $value){
                $user = M('User')->field('avatar_url,nickname')->where(array('id' => $value['sid']))->find();
                $user['avatar_url'] = $this->www_url . $user['avatar_url'];
                $sub_time = $value['sub_time']==0?$value['end_time']:$value['sub_time'];
                $data[$key]['sub_time'] = date("Y-m-d", $sub_time);
                $data[$key] = array_merge($data[$key], $user);
            }
            $this->ajaxReturn(array(
                'code'  => 1,
                'msg'   => '消息获取成功',
                'data'  => $data,
                'total_count' => $page_data['total'],
            ));
        }
        $this->seller->returnAjax(0, '暂无消息');
    }
    
    /**
     * 模特查看卖家邀请的体验
     * @param type $invite_id 体验报告id
     */
    public function viewExperience($invite_id) {
        if($invite_id == 0){
            $this->seller->returnAjax(0, '接口参数错误');
        }
        $experience = M('Invite_experience')->field('sid, title, end_time, total_fee, 
            product_num, remark, return_type, value， ensure_moeny_paid')->where(array('id' => $invite_id))->find();
        if(! empty($experience)){
            $user = M('User')->field('avatar_url')->where(array('id' => $experience['sid']))->find();
            $experience['avatar_url'] = $this->www_url . $user['avatar_url'];
            $this->seller->returnAjax(1, 获取体验详情成功, $experience);
        }
        $this->seller->returnAjax(0, '获取体验详情失败');
    }
    
    /**
     * 模特确认接受卖家邀请的体验
     * @param type $invite_id
     */
    public function confirmExperience($invite_id=0) {
        if($invite_id == 0){
            $this->seller->returnAjax(0, '接口参数错误');
        }
        $invite = M('Invite_experience')->where(array('id' => $invite_id, 'status' => 1))->find();
        if(!empty($invite)){
            M('Invite_experience')->where(array('id' => $invite_id))->save(array('status' => 4));
            $this->seller->sendMsg($invite['sid'], 2, '模特接受了您邀请的体验');
            $this->seller->returnAjax(1, '成功');
        }
        $this->seller->returnAjax(0, '已被卖家取消或该记录不存在');
    }

    /**
     * 模特拒绝卖家邀请体验
     * @param type $invite_id 体验记录id
     */
    public function refuseExperience($invite_id=0){
        if($invite_id == 0){
            $this->seller->returnAjax(0, '接口参数错误');
        }
        $invite = M('Invite_experience')->where(array('id' => $invite_id, 'status' => 1))->find();
        if(!empty($invite)){
            M('Invite_experience')->where(array('id' => $invite_id))->save(array('status' => 3));
            $this->seller->sendMsg($invite['sid'], 2, '模特拒绝了您邀请的体验');
            $this->seller->returnAjax(1, '拒绝成功');
        }
        $this->seller->returnAjax(0, '已被卖家取消或该记录不存在');
    }

    /**
     * 模特提交体验报告
     * @param type $invite_id   体验报告id
     * @param type $experience  体验报告文本内容
     */
    public function submitExperience($invite_id=0, $experience='') {
        if($invite_id == 0){
            $this->seller->returnAjax(0, '接口参数错误');
        }
        $imageUpload = new ImageController();
        $data = $imageUpload->upload('experience', true);
        //上传成功，更新数据库
        if($data['code'] == 1){ 
            $tmp = array('id' => $invite_id, 'experience' => $experience, 'sub_time' => NOW_TIME, 'status' => 6);
            M('Invite_experience')->save($tmp);
            //图片信息存储
            $pic = M('Experience_pic');
            foreach ($data['data'] as $key => $value){
                $pic_type = $key==0 ? 1 : 0;    //如果是第一张图片type为1
                $pic_data = array('invite_id' => $invite_id, 'pic_url' => $value, 'type' => $pic_type);
                $pic->add($pic_data);
                //修改返回url值
                $data['data'][$key] = $this->www_url . $value;
            }
            $invite = M('Invite_experience')->field('sid')->where(array('id' => $invite_id, 'status' => 1))->find();
            $this->seller->sendMsg($invite['sid'], 2, '模特已提交体验报告，请查看');
        }
        $this->ajaxReturn($data);
    }

    /**
     * 模特是否推荐体验内容到试衣间
     * @param type $is_recommend    是1否0推荐
     * @param type $reason          理由
     * @param type $invite_id       体验id
     */
    public function recommend($is_recommend, $reason, $invite_id=0) {
        if($invite_id == 0){
            $this->seller->returnAjax(0, '接口参数错误');
        }
        $recommend = array('id' => $invite_id, 'is_recommend' => $is_recommend, 'reason' => $reason, 'recommend_time' => NOW_TIME);
        if(M('Invite_experience')->save($recommend)){
            $this->seller->returnAjax(1, '操作成功');
        }
        $this->seller->returnAjax(0, '操作失败');
    }
}